home *** CD-ROM | disk | FTP | other *** search
- Path: cs.mu.OZ.AU!bounce-back
- From: clamage@Eng.Sun.COM (Steve Clamage)
- Newsgroups: comp.std.c++
- Subject: Re: String value of enum
- Date: 14 Mar 96 05:22:00 GMT
- Organization: Sun Microsystems Inc., Mountain View, CA
- Approved: fjh@cs.mu.oz.au
- Message-ID: <4i8a38$2qq@engnews1.Eng.Sun.COM>
- References: <4i5sf3$89c@hermes.is.co.za> <Do81tp.H9u@rsvl.unisys.com>
- NNTP-Posting-Host: munta.cs.mu.oz.au
- X-Original-Date: 14 Mar 1996 05:16:24 GMT
- Return-Path: <daemon@meeker.UCAR.EDU>
- X-Newsreader: NN version 6.5.0 #21 (NOV)
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMUetEOEDnX0m9pzZAQE11wF+LnWI9dP8nbJ90pXxXmBbqvU30n3CRQ58
- DZTpRiDEjEHhxdgnTDqc3XUgwvqpG8gT
- =iCQy
- Originator: fjh@munta.cs.mu.OZ.AU
-
- mtm4@rsvl.unisys.com (Michael McCormick) writes:
-
- >"W. Dicks" <wd@isis.co.za> shared the following on 13 Mar 96 07:31:13 GMT:
-
- >>The system that I'm working often needs to know the string
- >>value of an enum. e.g. enum week{MON=1, ..., SUN} it; it =
- >>TUE;
- >>Then when this enum is passed as a parameter the function
- >>should return the string based on the enum. For instance,
- >>weekImage(it) will return "TUE". Is it not possible that such
- >>a functionality can be built into enums?
-
- >What you are essentially asking for is language support for converting
- >a label into a string respresentation of its name. That is a very
- >unusual feature to find in any language, since it would require the
- >compiler to place the symbolic dictionary in the executable file.
-
- It would not in fact be difficult to do, since only a table of
- strings per enum type would be needed. The information is normally
- put into debug info if you compile in debug mode anyway so the
- debugger can display the labels instead of the values. A language
- mechanism to make that data always available is probably too
- expensive a price for every program to pay in order to support
- this requirement which isn't very common and which can be solved
- easily in source code.
-
-
- >Since enum is by definition an integral type, why not assign values to your
- >enumerators that can be used as indexes into a string?:
-
- > enum week {MON=0,TUE=4,WED=8,THU=12,FRI=16,SAT=20,SUN=24};
- > const char * days = "MON\0TUE\0WED\0THU\0FRI\0SAT\0SUN";
-
- > char const * weekImage(week day) = days[day];
-
- I would suggest instead this approach, which does not require you
- to count characters:
- const char* days[] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
- It "wastes" 7 extra pointers, but is easier to write and maintain.
-
- The more general case of non-contiguous enum values could be handled
- by a table of value/string pairs and a lookup function. The standard
- "map" class will do it all for you.
- --
- Steve Clamage, stephen.clamage@eng.sun.com
- ---
- [ comp.std.c++ is moderated. To submit articles: try just posting with ]
- [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
- [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
- [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
- [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
-